<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">角色列表</div>
                <div class="layui-card-body">
                    <div class="layui-btn-group test-table-operate-btn" style="margin-bottom: 10px;">
                        <button class="layui-btn" data-type="addRole">添加角色</button>
                    </div>

                    <table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate"></table>

                    <script type="text/html" id="test-table-operate-barDemo">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="auth">分配权限</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="addmodal" style="display: none;padding: 20px;">
    <form class="layui-form" action="" lay-filter="component-form-group">

        <div class="layui-form-item">
            <label class="layui-form-label">角色名称</label>
            <div class="layui-input-block">
                <input type="text" name="title" lay-verify="required" placeholder="请输入角色名称" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-inline" style="left: 0;">
                    <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="addSubmit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>
    </form>

</div>

<div id="editmodal" style="display: none;padding: 20px;">
    <form class="layui-form" action="" lay-filter="editgoods-form">

        <div class="layui-form-item">
            <label class="layui-form-label">角色名称</label>
            <div class="layui-input-block">
                <input type="text" name="title" id="editRoleName" lay-verify="required" autocomplete="off" class="layui-input">
                <input type="hidden" name="id" id="editRoleId">
            </div>
        </div>

        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-inline" style="left: 0;">
                    <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="editSubmit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>
    </form>
</div>

<div id="authmodal" style="display: none;padding: 20px;">
    <form class="layui-form" action="" lay-filter="auth-form">

        <input type="hidden" id="editAuthId" name="id">

        <div class="layui-form-item">
            <label class="layui-form-label">选择权限</label>
            <div class="layui-input-block">
                <div id="LAY-auth-tree-index"></div>
            </div>
        </div>

        <div class="layui-form-item layui-layout-admin" style="padding-top: 20px;">
            <div class="layui-input-block">
                <div class="layui-inline" style="left: 0;">
                    <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="editAuth">立即提交</button>
                </div>
            </div>
        </div>

    </form>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>

<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index'
        ,authtree: 'authtree'
    }).use(['index','form','authtree','table','laydate','set'], function(){
        $ = layui.jquery;
        var table = layui.table
            ,admin = layui.admin
            ,element = layui.element
            ,layer = layui.layer
            ,laydate = layui.laydate
            ,form = layui.form
            ,authtree = layui.authtree;


        table.render({
            elem: '#test-table-operate'
            ,url: "{:url('Role/roleList')}"
            ,cols: [[
              { align:'center',width:100,field:'id', title: 'ID'}
             ,{ align:'center',field:'title',  title: '角色名称'}
             //,{ align:'center',field:'level',  title: '等级（点击修改）',edit: 'text',style:'cursor: pointer;'}
             ,{ align:'center', title: '操作', fixed: 'right', toolbar: '#test-table-operate-barDemo'}
    ]]
    ,page: false
    });

        form.render(null, 'component-form-group');

        //监听工具条
        table.on('tool(test-table-operate)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){                            //删除操作
                layer.confirm('确认删除吗', function(index){
                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: "{:url('Role/del')}",
                        type: 'post',
                        dataType: 'text',
                        data: {id:data.id},
                        success: function(msg){
                            layer.close(loading);
                            if (msg === "1") {
                                layer.msg('删除成功',{icon:1,time:1000},function(){
                                    obj.del();
                                    layer.close(index);
                                });

                            }else{
                                layer.msg(msg,{icon:2,time:2000});
                            }
                        }
                    })

                });
            }else if(obj.event === 'edit'){                           //修改操作
                $('#editRoleName').val(data.title);
                $('#editRoleId').val(data.id);
                var editmodal = layer.open({
                    title:'修改角色'
                    ,type: 1
                    ,shadeClose: true
                    ,area: admin.screen() < 2 ? ['80%'] : ['700px']
                    ,content: $('#editmodal')
                });
            }else if(obj.event === 'auth'){
                var loading = layer.load(1, {
                    shade: [0.1,'#fff'] //0.1透明度的白色背景
                });
                $('#editAuthId').val(data.id);
                $('#LAY-auth-tree-index').html('');
                $.ajax({
                    url: "{:url('Role/auth')}",
                    type:'post',
                    dataType: 'json',
                    data:{id:data.id},
                    success: function(data){
                        // 渲染时传入渲染目标ID，树形结构数据（具体结构看样例，checked表示默认选中），以及input表单的名字
                        authtree.render('#LAY-auth-tree-index', data.data.trees, {inputname: 'authids[]', layfilter: 'lay-check-auth', openall: true});

                        // 监听自定义lay-filter选中状态，PS:layui现在不支持多次监听，所以扩展里边只能改变触发逻辑，然后引起了事件冒泡延迟的BUG，要是谁有好的建议可以反馈我
                        form.on('checkbox(lay-check-auth)', function(data){
                            // 注意这里：需要等待事件冒泡完成，不然获取叶子节点不准确。
                            setTimeout(function(){
                                // 获取选中的叶子节点
                                var leaf = authtree.getLeaf('#LAY-auth-tree-index');
                            }, 100);
                        });
                        layer.close(loading);
                        var authmodal = layer.open({
                            title:'权限分配'
                            ,type: 1
                            ,shadeClose: true
                            ,area: ['500px','600px']
                            ,content: $('#authmodal')
                        });
                    }
                });

            }
        });
        //添加提交
        form.on('submit(addSubmit)', function(data){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.ajax({
                url: "{:url('Role/add')}",
                type: 'post',
                dataType: 'text',
                data: {data:data.field},
                success: function(info){
                    if (info === "1") {
                        layer.msg('添加成功',{icon:1,time:2000},function(){
                            window.location.reload();
                        });
                    }else{
                        layer.close(loading);
                        layer.msg('添加失败',{icon:2,time:1000})
                    }
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //修改提交
        form.on('submit(editSubmit)', function(data){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.ajax({
                url: "{:url('Role/editRole')}",
                type: 'post',
                dataType: 'text',
                data: {data:data.field},
                success: function(info){
                    if (info === "1") {
                        layer.msg('修改成功',{icon:1,time:2000},function(){
                            window.location.reload();
                        });
                    }else{
                        layer.close(loading);
                        layer.msg('修改失败',{icon:2,time:1000})
                    }
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //权限分配提交
        form.on('submit(editAuth)', function(data){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.ajax({
                url: "{:url('Role/authSet')}",
                type: 'post',
                dataType: 'text',
                data: {data:data.field},
                success: function(info){
                    if (info === "1") {
                        layer.msg('设置成功',{icon:1,time:1500},function(){
                            layer.closeAll();
                        });
                    }else{
                        layer.close(loading);
                        layer.msg(info,{icon:2,time:1500})
                    }
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //监听单元格编辑
        table.on('edit(test-table-operate)', function(obj) {
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.ajax({
                url: "{:url('Role/editLevel')}",
                type: 'post',
                dataType: 'text',
                data: {id:obj.data.id,level:obj.value},
                success: function(msg){
                    layer.close(loading);
                    if (msg === "1") {
                        layer.close(loading);
                    }else{
                        layer.close(loading);
                        layer.msg('修改失败',{icon:2,time:1500});
                    }

                }
            })

        });

        var $ = layui.$, active = {
            addRole: function(){
                layer.open({
                    title:'添加角色'
                    ,type: 1
                    ,shadeClose: true
                    ,area: admin.screen() < 2 ? ['80%'] : ['700px']
                    ,content: $('#addmodal')
                });

            }
        };

        $('.test-table-operate-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

</script>
</body>
</html>